package com.outdoor.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.outdoor.common.result.PageResult;
import com.outdoor.pojo.dto.NodePageQueryDTO;
import com.outdoor.pojo.entity.Node;
import com.outdoor.pojo.entity.NodeAttachment;
import com.outdoor.pojo.vo.NodeVO;
import com.outdoor.server.mapper.NodeAttachmentMapper;
import com.outdoor.server.mapper.NodeMapper;
import com.outdoor.server.service.NodeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private NodeAttachmentMapper nodeAttachmentMapper;

    /**
     * 分页查询节点信息
     * @param nodePageQueryDTO
     * @return
     */
    public PageResult page(NodePageQueryDTO nodePageQueryDTO) {
        PageHelper.startPage(nodePageQueryDTO.getPage(), nodePageQueryDTO.getPageSize());
        nodePageQueryDTO.setType(2);
        Page<Node>  page = nodeMapper.pageQuery(nodePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询节点详细信息
     * @param id
     * @return
     */
    public NodeVO getById(Long id) {
        Node node = nodeMapper.selectById(id);
        List<NodeAttachment> attachments = nodeAttachmentMapper.selectByNodeId(id);
        NodeVO nodeVO = new NodeVO();
        BeanUtils.copyProperties(node, nodeVO);
        nodeVO.setAttachments(attachments);
        return nodeVO;
    }
}
